package com.xcity.tools.template;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class LandConvert {

	static class Land {
		@Comment("地标id")
		int id;
		@Comment("地标名称")
		String name;
		@Comment("描述")
		String desc;
		@Comment("图片")
		String image;
		@Comment("收购所消耗的货币类型(1:现金;2:钻石)")
		byte costType;
		List<LandLevelUp> levelUpSettings = new ArrayList<>();
	}
	
	static class LandLevelUp {
		@Comment("地标id")
		int landId;
		@Comment("地标等级")
		int level;
		@Comment("保护时间(秒)")
		int cd;
		@Comment("收购价格")
		int cost;
		@Comment("每分钟收益")
		int income;
		@Comment("最大收益")
		int maxIncome;
		@Comment("价值(计算固资使用)")
		int price;
	}
	
	static final File PARENT = new File("F:/prj_java/city_doc/策划/配置表");
	
	public static void main(String[] args) throws Exception {
		long time = System.currentTimeMillis();
		List<Land> list = parse(new File(PARENT, "Deprecated/build.xlsx"));
		convert(list, new File(PARENT, "land.xlsx"));
		System.out.println("ok=" + (System.currentTimeMillis() - time));
	}
	
	static void convert(List<Land> landList, File file) throws Exception {
		Workbook book = new XSSFWorkbook();
		
		convert0(book, landList);
		convert1(book, landList);
		
		FileOutputStream out = new FileOutputStream(file);
		book.write(out);
		out.flush();
		out.close();
		book.close();
	}
	
	static void convert0(Workbook book, List<Land> landList) throws Exception {
		Sheet sheet = book.createSheet("地标列表");
		int rowOffset = writeHead(sheet, Land.class);
		
		Field[] fields = Land.class.getDeclaredFields();
		for (int i = 0; i < landList.size(); i++) {
			Land land = landList.get(i);
			Row row = sheet.createRow(rowOffset++);
			int col = 0;
			for (int j = 0; j < fields.length; j++) {
				Field f = fields[j];
				if (f.getDeclaredAnnotation(Comment.class) != null) {
					Cell cell = row.createCell(col++);
					Class<?> type = f.getType();
					if (type == byte.class) {
						cell.setCellValue(f.getByte(land));
					} else if (type == int.class) {
						cell.setCellValue(f.getInt(land));
					} else {
						cell.setCellValue(f.get(land).toString());
					}
				}
			}
		}
	}
	
	static int writeHead(Sheet sheet, Class<?> clazz) {
		int rowOffset = 0;
		Row head0 = sheet.createRow(rowOffset++);
		Row head1 = sheet.createRow(rowOffset++);
		Row head2 = sheet.createRow(rowOffset++);
		
		Field[] fields = clazz.getDeclaredFields();
		int col = 0;
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			Comment comment = f.getDeclaredAnnotation(Comment.class);
			if (comment != null) {
				Cell cell = head0.createCell(col);
				cell.setCellValue(f.getName());
				
				cell = head1.createCell(col);
				cell.setCellValue(f.getType().getSimpleName());
				
				cell = head2.createCell(col);
				cell.setCellValue(comment.value());
				col++;
			}
		}
		return rowOffset;
	}
	
	static void convert1(Workbook book, List<Land> landList) throws Exception {
		Sheet sheet = book.createSheet("地标收购");
		Field[] fields = LandLevelUp.class.getDeclaredFields();
		int rowOffset = writeHead(sheet, LandLevelUp.class);
		for (Land land : landList) {
			List<LandLevelUp> list = land.levelUpSettings;
			for (LandLevelUp llu : list) {
				Row row = sheet.createRow(rowOffset++);
				int col = 0;
				for (Field f : fields) {
					if (f.getDeclaredAnnotation(Comment.class) != null) {
						Cell cell = row.createCell(col++);
						cell.setCellValue(f.getInt(llu));
					}
				}
			}
		}
	}
	
	static List<Land> parse(File file) throws Exception {
		Workbook book = getWorkbook(file);
		Sheet sheet = book.getSheetAt(1);
		int from = 3;//sheet.getFirstRowNum();
		int to = sheet.getLastRowNum();
		List<Land> landList = new ArrayList<Land>();
		for (int i = from; i <= to; i++) {
			Row row = sheet.getRow(i);
			Land land = new Land();
			landList.add(land);
			for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
				Cell cell = row.getCell(j);
//				if (cell == null)
//					break;
				String value;
				if (cell == null) {
					value = "";
				} else {
					switch (cell.getCellType()) {
						case Cell.CELL_TYPE_NUMERIC:
							value = String.valueOf(cell.getNumericCellValue());
							break;
						case Cell.CELL_TYPE_STRING:
							value = cell.getStringCellValue();
							break;
						case Cell.CELL_TYPE_BOOLEAN:
							value = String.valueOf(cell.getBooleanCellValue());
							break;
						default:
							value = "";
					}
				}
				value = value.trim();
				if (j == 0) { // id
				} else if (j == 1) { // land id
					land.id = Double.valueOf(value).intValue();
				} else if (j == 2) { // name
					land.name = value;
				} else if (j == 3) { // image
					land.image = value;
				} else if (j == 4) { // cost unlock
				} else if (j == 5) { // type
				} else if (j == 6) { // buyType
					land.costType = Double.valueOf(value).byteValue();
				} else if (j == 7) { // buyPrice
				} else if (j == 8) { // desc
					land.desc = value;
				} else { // level up
					if (value.isEmpty()) {
						break;
					}
					String[] vals = value.toString().trim().split("\\|");
//					建筑CD|升级消耗|收益|最大收益|风险值|出售价格
					List<LandLevelUp> list = land.levelUpSettings;
					LandLevelUp lvlUp = new LandLevelUp();
					list.add(lvlUp);
					lvlUp.landId = land.id;
					lvlUp.level = list.size();
					lvlUp.cd = (int) (Double.valueOf(vals[0]) / 1000);
					lvlUp.cost = Double.valueOf(vals[1]).intValue();
					lvlUp.income = Double.valueOf(vals[2]).intValue();
					lvlUp.maxIncome = Double.valueOf(vals[3]).intValue();
					lvlUp.price = land.costType == 1 ? lvlUp.cost : (lvlUp.cost * 800);
				}
			}
		}
		
		book.close();
		return landList;
	}
	
	static Workbook getWorkbook(File file) throws Exception {
		if (file.getName().endsWith("xlsx")) {
			return new XSSFWorkbook(file);
		}
		return new HSSFWorkbook(new FileInputStream(file));
	}
	
}
